From 63291a543aac293d91f2f64faf47bb48886b59c4 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Wed, 14 May 2025 00:13:48 -0400
Subject: When there's no message in view, update last read time based on the
 channel's creation time.

---
 ui/routes/(app)/ch/[channel]/+page.svelte | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

(limited to 'ui/routes/(app)/ch/[channel]/+page.svelte')

diff --git a/ui/routes/(app)/ch/[channel]/+page.svelte b/ui/routes/(app)/ch/[channel]/+page.svelte
index 33a9bdf..1130cd4 100644
--- a/ui/routes/(app)/ch/[channel]/+page.svelte
+++ b/ui/routes/(app)/ch/[channel]/+page.svelte
@@ -10,9 +10,10 @@
   const { session, outbox } = data;
   let activeChannel;
 
-  const channel = $derived(page.params.channel);
-  const messages = $derived(session.messages.filter((message) => message.channel === channel));
-  const unsent = $derived(outbox.messages.filter((message) => message.channel === channel));
+  const channelId = $derived(page.params.channel);
+  const channel = $derived(session.channels.get(channelId));
+  const messages = $derived(session.messages.filter((message) => message.channel === channelId));
+  const unsent = $derived(outbox.messages.filter((message) => message.channel === channelId));
   const deleted = $derived(outbox.deleted.map((message) => message.messageId));
   const unsentSkeletons = $derived(
     unsent.map((message) => message.toSkeleton($state.snapshot(session.currentUser)))
@@ -45,10 +46,8 @@
 
   function setLastRead() {
     const lastInView = getLastVisibleMessage();
-    if (lastInView) {
-      const at = DateTime.fromISO(lastInView.dataset.at);
-      session.local.updateLastReadAt(channel, at);
-    }
+    const at = !!lastInView ? DateTime.fromISO(lastInView.dataset.at) : channel.at;
+    session.local.updateLastReadAt(channelId, at);
   }
 
   $effect(() => {
@@ -76,7 +75,7 @@
   }
 
   async function sendMessage(message) {
-    outbox.postToChannel(channel, message);
+    outbox.postToChannel(channelId, message);
   }
 
   async function deleteMessage(id) {
-- 
cgit v1.2.3

